﻿using AntdUI;
using AntdUI.Svg;
using Lima.ResumeSys.Models;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Lima.ResumeSys.Util
{

    public class TreeBuilder
    {
        public void BindTreeViewWithParentId(AntdUI.Tree treeView, List<Organization> data, string userOrgId)
        {
            treeView.Items.Clear();

            // 获取所有根节点（ParentId为null或0的节点）
            // var rootNodes = data.Where(x => string.IsNullOrWhiteSpace(x.ParCode)).ToList();
            var rootNode = data.Where(x => x.ID == userOrgId).FirstOrDefault();

            if (rootNode == null)
            {
                treeView.Items = null;
                return;
            }

            var treeNode = new AntdUI.TreeItem() { ID = rootNode.ID, Name = rootNode.Code, Text = rootNode.OrgName };
            AddChildNodes(treeNode, data);
            treeView.Items.Add(treeNode);


            //foreach (var rootNode in rootNodes)
            //{
            //    var treeNode = new AntdUI.TreeItem() { ID = rootNode.ID, Name = rootNode.Code, Text = rootNode.OrgName };
            //    AddChildNodes(treeNode, data);
            //    treeView.Items.Add(treeNode);
            //}
        }

        private void AddChildNodes(TreeItem parentNode, List<Organization> allData)
        {
            var childNodes = allData.Where(x => !string.IsNullOrWhiteSpace(x.ParCode) && x.ParCode.Equals(parentNode.ID)).ToList();

            foreach (var childData in childNodes)
            {
                var childTreeNode = new TreeItem() { ID = childData.ID, Name = childData.Code, Text = childData.OrgName, PARENTITEM = parentNode };

                parentNode.Sub.Add(childTreeNode);
                AddChildNodes(childTreeNode, allData); // 递归添加子节点
            }
        }

        /// <summary>
        /// 下拉框树形数组
        /// </summary>
        /// <param name="dropdown"></param>
        /// <param name="data"></param>
        public void BindDropDownWithParentId(AntdUI.Select dropdown, List<Organization> data, string userOrgId)
        {
            dropdown.Items.Clear();

            // 获取所有根节点（ParentId为null或0的节点）
            //var rootNodes = data.Where(x => string.IsNullOrWhiteSpace(x.ParCode)).ToList();
            var rootNode = data.Where(x => x.ID == userOrgId).FirstOrDefault();

            if (rootNode == null)
            {
                dropdown.Items = null;
                return;
            }
            //var treeNode = new AntdUI.TreeItem() { ID = rootNode.ID, Name = rootNode.Code, Text = rootNode.OrgName };

            var selectNode = new AntdUI.SelectItem(rootNode.ID)
            {
                Text = rootNode.OrgName
            };
            AddDropDownChildNodes(selectNode, data);
            dropdown.Items.Add(selectNode);
            //foreach (var rootNode in rootNodes)
            //{
            //    var selectItem = new AntdUI.SelectItem(rootNode.ID)
            //    {
            //        Text = rootNode.OrgName
            //    };
            //    AddDropDownChildNodes(selectItem, data);
            //    dropdown.Items.Add(selectItem);
            //}
        }

        private void AddDropDownChildNodes(AntdUI.SelectItem parentNode, List<Organization> allData)
        {
            var childNodes = allData.Where(x => !string.IsNullOrWhiteSpace(x.ParCode) && x.ParCode.Equals(parentNode.Tag)).ToList();

            var tempList = new List<object>();

            foreach (var childData in childNodes)
            {

                var childSelectItem = new AntdUI.SelectItem(childData.ID)
                {
                    Text = childData.OrgName
                };
                tempList.Add(childSelectItem);

                parentNode.Sub = tempList;

                AddDropDownChildNodes(childSelectItem, allData); // 递归添加子节点
            }
        }


    }
}
